package com.bolao.bets;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

import com.Adapter.CustomTeamAdapter;
import com.Adapter.MatchAdapter;
import com.CustomClasses.InsertMySQL;
import com.bolao.MainTabActivity;
import com.bolao.R;
import com.entidades.Match;
import com.entidades.Team;

public class BetsMatchsFragment extends Fragment {

	private static final String MATCH_ID = "matchid";
	private Spinner spinnerSortByTeam;
	private ListView listViewMatchs;
	private MatchAdapter mAdapter;
	private MatchAdapter mAdapterAll;
	private Button sendButton;
	private OnClickListener buttonListener;
	private static final String tableName = "matchs_bets";

	private static List<String> getList(){
		List<String> ls = new ArrayList<String>();
		ls.add(MATCH_ID);
		ls.add("teamAScore");
		ls.add("teamBScore");
		return ls;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)  {
		// Inflate the menu; this adds items to the action bar if it is present.
		View rootView = inflater.inflate(
				R.layout.match_bet_layout, container, false);
		super.onCreateView(inflater, container, savedInstanceState);
	    
		spinnerSortByTeam = (Spinner) rootView.findViewById(R.id.spinnerSortByTeam);
		mAdapterAll = new MatchAdapter(container.getContext(), Match.getGroupMatchs(), true);
		CustomTeamAdapter tAdapeter = new CustomTeamAdapter(container.getContext(), Team.getTeams());
		spinnerSortByTeam.setAdapter(tAdapeter);
		listViewMatchs = (ListView) rootView.findViewById(R.id.listViewMatchBets);
		listViewMatchs.setAdapter(mAdapterAll);
		sendButton = (Button) rootView.findViewById(R.id.buttonTeamSubmit);
		spinnerSortByTeam.setOnItemSelectedListener(new OnTeamSelected());
		
		buttonListener = new View.OnClickListener() {
		    @Override
		    public void onClick(View v) {
				    					    	
				    		new InsertMySQL(tableName, getList(), MainTabActivity.getUser().getId(), Match.getAllUserMatchsBet(Match.getGroupMatchs())).execute();
						
				    }		    
		};
		sendButton.setOnClickListener(buttonListener);

		return rootView;
	}
	
	class OnTeamSelected implements OnItemSelectedListener{
		@Override
		public void onItemSelected(AdapterView<?> adapter, View arg1, int arg2,
				long arg3) {
			Team team = (Team) adapter.getItemAtPosition(arg2);
			if(team!=null){
			mAdapter = new MatchAdapter(getActivity().getApplicationContext(), Match.getMatchesOfTeam(team), true);
			listViewMatchs.setAdapter(mAdapter);
			} else onNothingSelected(adapter);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			listViewMatchs.setAdapter(mAdapterAll);
			sendButton.setOnClickListener(buttonListener);
		}
	}
	
	
}
